<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="电视设备号" prop="tvSn">
        <el-input
          v-model="queryParams.tvSn"
          placeholder="请输入电视设备号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="电视所属机构id" prop="tvDeptId">
        <el-input
          v-model="queryParams.tvDeptId"
          placeholder="请输入电视所属机构id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构名称" prop="tvDeptName">
        <el-input
          v-model="queryParams.tvDeptName"
          placeholder="请输入机构名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级ID" prop="tvGradeId">
        <el-input
          v-model="queryParams.tvGradeId"
          placeholder="请输入年级ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级名称" prop="tvGradeName">
        <el-input
          v-model="queryParams.tvGradeName"
          placeholder="请输入年级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级ID" prop="tvClassId">
        <el-input
          v-model="queryParams.tvClassId"
          placeholder="请输入班级ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级名称" prop="tvClassName">
        <el-input
          v-model="queryParams.tvClassName"
          placeholder="请输入班级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="机构名称" prop="institutionId">
        <el-select v-model="queryParams.institutionId" filterable placeholder="请选择机构" clearable size="small" @change="selectGrade">
          <el-option
            v-for="dept in deptList"
            :key="dept.institutionId"
            :label="dept.institutionName"
            :value="dept.institutionId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="年级名称" prop="gradeId">
        <el-select v-model="queryParams.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
          <el-option
            v-for="dept in gradeList"
            :key="dept.gradeId"
            :label="dept.gradeName"
            :value="dept.gradeId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班级名称" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable size="small" >
          <el-option
            v-for="dept in classList"
            :key="dept.classId"
            :label="dept.className"
            :value="dept.classId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tongban:tvdevice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tongban:tvdevice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tongban:tvdevice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tongban:tvdevice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tvdeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="电视设备ID" align="center" prop="tvDeviceId" />-->
      <el-table-column label="电视设备号" align="center" prop="tvSn" />
<!--      <el-table-column label="电视所属机构id" align="center" prop="tvDeptId" />-->
      <el-table-column label="机构名称" filterable align="center" prop="tvDeptName" />
<!--      <el-table-column label="年级ID" align="center" prop="tvGradeId" />-->
      <el-table-column label="年级名称" align="center" prop="tvGradeName" />
<!--      <el-table-column label="班级ID" align="center" prop="tvClassId" />-->
      <el-table-column label="班级名称" align="center" prop="tvClassName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tongban:tvdevice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tongban:tvdevice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电视设备表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="电视设备号" prop="tvSn" v-if="isShow">
          <el-input v-model="form.tvSn" placeholder="请输入电视设备号" />
        </el-form-item>
<!--        <el-form-item label="电视所属机构id" prop="tvDeptId">
          <el-input v-model="form.tvDeptId" placeholder="请输入电视所属机构id" />
        </el-form-item>
        <el-form-item label="机构名称" prop="tvDeptName">
          <el-input v-model="form.tvDeptName" placeholder="请输入机构名称" />
        </el-form-item>-->
        <el-form-item label="机构" prop="institutionId">
          <el-select v-model="form.institutionId" placeholder="请选择机构" clearable size="small" @change="selectGrade">
            <el-option
              v-for="dept in deptList"
              :key="dept.institutionId"
              :label="dept.institutionName"
              :value="dept.institutionId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="年级ID" prop="tvGradeId">
          <el-input v-model="form.tvGradeId" placeholder="请输入年级ID" />
        </el-form-item>
        <el-form-item label="年级名称" prop="tvGradeName">
          <el-input v-model="form.tvGradeName" placeholder="请输入年级名称" />
        </el-form-item>-->
        <el-form-item label="年级" prop="gradeId">
          <el-select v-model="form.gradeId" placeholder="请选择年级" clearable size="small" @change="selectClass">
            <el-option
              v-for="dept in gradeList"
              :key="dept.gradeId"
              :label="dept.gradeName"
              :value="dept.gradeId"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="班级ID" prop="tvClassId">
          <el-input v-model="form.tvClassId" placeholder="请输入班级ID" />
        </el-form-item>
        <el-form-item label="班级名称" prop="tvClassName">
          <el-input v-model="form.tvClassName" placeholder="请输入班级名称" />
        </el-form-item>-->
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" placeholder="请选择班级" clearable size="small">
            <el-option
              v-for="dept in classList"
              :key="dept.classId"
              :label="dept.className"
              :value="dept.classId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTvdevice, getTvdevice, delTvdevice, addTvdevice, updateTvdevice, exportTvdevice } from "@/api/tongban/tvdevice";
import {listInstitutionData} from "@/api/tongban/institution";
import {listInstitutionGrade} from "@/api/tongban/grade";
import {listInstitutionClass} from "@/api/tongban/class";

export default {
  name: "Tvdevice",
  components: {
  },
  data() {
    return {
      isShow : true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      deptList: [],
      gradeList: [],
      classList: [],
      // 电视设备表表格数据
      tvdeviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tvSn: null,
        tvDeptId: null,
        tvDeptName: null,
        tvGradeId: null,
        tvGradeName: null,
        tvClassId: null,
        tvClassName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    listInstitutionData().then(res => {
      this.deptList = res.data;
      console.log(this.deptList);
    });
  },
  methods: {
    /** 查询年级*/
    selectGrade(id){
      listInstitutionGrade(id).then(res =>{
        this.gradeList = res.data;
        console.log(id);
      })
    },

    /** 查询班级*/
    selectClass(id){
      listInstitutionClass(id).then(response => {
        this.classList = response.data;
        //this.form.classId = this.classList[0];
        console.log(id);
      })
    },

    /** 查询电视设备表列表 */
    getList() {
      this.loading = true;
      listTvdevice(this.queryParams).then(response => {
        this.tvdeviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tvDeviceId: null,
        tvSn: null,
        tvDeptId: null,
        tvDeptName: null,
        tvGradeId: null,
        tvGradeName: null,
        tvClassId: null,
        tvClassName: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tvDeviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isShow = true;
      this.reset();
      this.open = true;
      this.title = "添加电视设备表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isShow = false;
      this.reset();
      const tvDeviceId = row.tvDeviceId || this.ids
      getTvdevice(tvDeviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电视设备表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.tvDeviceId != null) {
            updateTvdevice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTvdevice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tvDeviceIds = row.tvDeviceId || this.ids;
      this.$confirm('是否确认删除电视设备表编号为"' + tvDeviceIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTvdevice(tvDeviceIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有电视设备表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTvdevice(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
